// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import {
  getComplianceRank,
  getComplianceStat,
  getSpecialLetter,
  getViolation,
  getFeedback,
} from '@/services/evaluate';
import initChart from '@/utils/initChart';
import { getFinanceBarOption } from '@/utils/echarts-figures.js';

export default {
  namespace: 'compliance',

  state: {
    stat: {},
    letter: [],
  },

  effects: {
    /**
     * 获取评价-信批合规-信批评级
     */
    *getRank({ payload }, { call, put }) {
      try {
        const res = yield call(getComplianceRank, payload);
        if (+res.code === 0) {
          const option = res.data.option; //getFinanceBarOption('信批评级', res.data.dates);
          console.log('生成的选项：', option);
          initChart(option, payload.chartId);
          // yield put({ type: 'setData', payload: {} });
        } else {
          message.error(res.message);
        }
      } catch (e) {
        console.log('失败', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
    /**
     * 获取评价-信批合规-本年度信披违规统计
     */
    *getStatistics({ payload }, { call, put }) {
      try {
        const res = yield call(getComplianceStat, payload);
        if (+res.code === 0) {
          initChart(res.data.content_industry_option, payload.industryRateId);
          initChart(res.data.content_all_option, payload.allRateId);
          initChart(res.data.deal_industry_option, payload.detailIndustryId);
          initChart(res.data.deal_all_option, payload.detailAllId);
        } else {
          message.error(res.message);
        }
      } catch (e) {
        console.log('获取统计数据出错：', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
    /**
     * 获取评价-信批合规-函件(特殊逻辑的监管函件)
     */
    *getLetter({ payload }, { call, put }) {
      try {
        const res = yield call(getSpecialLetter, payload);
        if (+res.code === 0) {
          yield put({
            type: 'setData',
            payload: {
              letter: res.data.data_list,
            },
          });
        } else {
          message.error(res.message);
        }
      } catch (e) {
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
    /**
     * 获取评价-信批合规-扣分细则
     */
    *getViolationDetail({ payload }, { call, put }) {
      try {
        const res = yield call(getViolation, payload);
        if (+res.code === 0) {
          yield put({
            type: 'setData',
            payload: {
              violation: res.data.data_list,
            },
          });
        } else {
          // message.error(res.message);
        }
      } catch (e) {
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
    /**
     * 获取评价-信批合规-监管反馈
     */
    *getFeedback({ payload }, { call, put }) {
      try {
        const res = yield call(getFeedback, payload);
        if (+res.code === 0) {
          yield put({
            type: 'setData',
            payload: {
              feedback: res.data.data_list,
            },
          });
        } else {
          // message.error(res.message);
        }
      } catch (e) {
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
  },
  reducers: {
    setData(state, { payload }) {
      const newState = {
        ...state,
        ...payload,
      };
      return newState;
    },
  },
};
